//
//  XiaoXiViewController.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/24.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "XiaoXiViewController.h"
#import "SYRequestAPI+SYXiaoXi.h"
#import "SYHouSanMingViewController.h"
#import "CHAlertView.h"
@interface XiaoXiViewController ()<CHAlertViewDelegate>

@end

@implementation XiaoXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MAINBACKCOLOR;
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(0, 20, APP_WIDTH, 1);
    lineView.backgroundColor = LINCOLOR;
    [self.view addSubview:lineView];
}
//290-75
- (TeacherLeftView *)leftMenuView{
    if (!_leftMenuView) {
        TeacherLeftView *view = [[TeacherLeftView alloc]initWithFrame:CGRectMake(0, 130, 215*fw, APP_HEIGHT-130) andButtonView:@[@" 收件箱"] andImageArray:@[@"xiaoxi_shoujianxiang.png",@"xiaoxi_fajianxiang.png"]];//,@" 发件箱"
        view.delegate = self;
        view.openButton.hidden = YES;
        _leftMenuView = view;
    }
    return _leftMenuView;
}
- (UIView *)baseView{
    if (!_baseView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor clearColor];
        _baseView = view;
    }
    return _baseView;
}
- (UIView *)baseLeftView{
    if (!_baseLeftView) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(0, 20, 215*fw, APP_HEIGHT-20);
        view.backgroundColor = [UIColor clearColor];
        _baseLeftView = view;
    }
    return _baseLeftView;
}
#pragma mark - 添加子视图
- (void)initSubView{
    self.view.backgroundColor = CONTENTBACKCOLOR;
    _allViewArray = [NSMutableArray new];
    for (int i = 0; i<2; i++) {
        if (i == 0) {
            _xiaoXiReciveVC = [[XiaoXiReciveVC alloc]init];
            _xiaoXiReciveVC.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
            _xiaoXiReciveVC.view.backgroundColor = [UIColor whiteColor];
            [_allViewArray addObject:_xiaoXiReciveVC];
        }else if (i==1){
            _xiaoXiSendVC = [[XiaoXiSendVC alloc]init];
            _xiaoXiSendVC.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
            _xiaoXiSendVC.view.backgroundColor = [UIColor whiteColor];
            [_allViewArray addObject:_xiaoXiSendVC];
        }else if (i == 2){
            
        }else{
            
        }
    }
    [self.view addSubview:self.baseLeftView];

    [_baseLeftView addSubview:self.leftMenuView];
    [_baseLeftView addSubview:self.titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@50);
    }];
    [_baseLeftView addSubview:self.addButton];
    [_addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->_baseLeftView.mas_right).offset(-10);
        make.centerY.equalTo(self->_titleLabel.mas_centerY);
        make.width.equalTo(@17);
        make.height.equalTo(@17);
    }];
    [_leftMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(self->_titleLabel.mas_bottom).offset(1);
        make.width.equalTo(@0).offset(215*fw);
        make.height.equalTo(@0).offset(APP_HEIGHT-130-100);
    }];
   
    [self.view addSubview:self.baseView];
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftMenuView.mas_right);
        make.top.equalTo(@20);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self mineClick:0];
    UIView *lineView = [UIView new];
    lineView.backgroundColor = LINCOLOR;
    [_baseLeftView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self->_titleLabel.mas_bottom);
        make.height.equalTo(@1);
    }];
}
- (void)initOption{
    //如果有消息显示红色按钮
    for (int i = 0; i<_allViewArray.count; i++) {
        UIView *redView = (UIView *)[self.leftMenuView viewWithTag:50+i];
        redView.hidden = YES;
    }
}
- (void)layout{
    
}
#pragma mark - 头部标题和添加按钮
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"消息";
        label.textAlignment = 1;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:16];
        _titleLabel = label;
    }
    return _titleLabel;
}
- (UIButton *)addButton{
    if (!_addButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"xiaoxi_add"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        button.hidden = YES;
        _addButton = button;
    }
    return _addButton;
}
- (void)addClick{
    [self alertView];
}
- (void)alertView{
    //暂时没有发送消息
//    [UIApplication sharedApplication].keyWindow.rootViewController.definesPresentationContext = YES;
//    CreateNewMessageView *view = [[CreateNewMessageView alloc]init];
//    view.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//    view.view.superview.frame = CGRectMake(0, 0,self.view.frame.size.width, APP_HEIGHT);
//    view.cancelBlock = ^{
//        [view dismissViewControllerAnimated:NO completion:nil];
//    };
//    view.confirmBlock = ^(NSString *content, NSMutableArray *classIDArray) {
//        NSLog(@"b内容 %@",content);
//        NSLog(@"班级ID %@",classIDArray);
//    };
//    [self presentViewController:view animated:NO completion:nil];

}
#pragma mark - 发送班级消息
- (void)sendClassMessageRequest{
    [SYRequestAPI sy_getNoticeSendServiceSendNotice:@"" and:@"" success:^(NSMutableArray *dataListArr) {
        NSLog(@"发送班级消息 %@",dataListArr);
    } fail:^(NSError *error) {
        NSLog(@"发送班级消息 %@",error);
    }];
}
#pragma mark - 左侧按钮点击
- (void)mineClick:(int)index{
    UIViewController *vc = _allViewArray[index];
    if (self.currentVC == _allViewArray[index]) {
        return;
    }else{
        [self addChildViewController:vc];
        vc.view.hidden = NO;
        [self.baseView addSubview:vc.view];
        [vc.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView.mas_left);
            make.right.equalTo(self.baseView.mas_right);
            make.top.equalTo(@0);
            make.bottom.equalTo(self.baseView.mas_bottom);
        }];
        //替换view
        [vc didMoveToParentViewController:self];
        [self.currentVC willMoveToParentViewController:nil];
        [self.currentVC removeFromParentViewController];
        [self.currentVC.view removeFromSuperview];
        self.currentVC.view.hidden = YES;
    }
  
}

- (void)quiteAcount:(UIButton *)sender{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定退出" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alert addAction:cancel];
    [alert addAction:confirm];
    [self presentViewController:alert animated:YES completion:nil];
    
}

@end
